package com.beamfield.controller.manage.steelplant;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.SteelplantProducetask;
import com.beamfield.model.SteelplantProducetaskDTO;
import com.beamfield.model.SteelplantStandardparts;
import com.beamfield.model.SteelplantStandardpartsDTO;
import com.beamfield.modelVo.AcceptanceStandardFilePathVo;
import com.beamfield.service.serviceimpl.manage.steelplant.SteelplantProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * 钢筋厂管理
 */
@Controller
@RequestMapping(value = "/cms/rebar/production")
public class ProductionController extends BaseController {

    /*技术交底及作业指导书   rebar\production\technicalGuidance
标准件大样图   rebar\production\standardPattern
生产任务单   rebar\production\productionTaskOrder
加工验收标准   rebar\production\acceptanceStandard

    * */
    @Autowired
    private SteelplantProductionService steelplantProductionService;


    @Value("${acceptanceStandardfilePath:D:\\BCS7-B12-DB\\}")
    private String acceptanceStandardfilePath;
    @Value("${acceptanceStandardUrl}")
    private String acceptanceStandardUrl;

    @RequestMapping(value = "/technicalGuidance")
    public ModelAndView technicalGuidance() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/standardPattern")
    public ModelAndView standardPattern() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/productionTaskOrder")
    public ModelAndView productionTaskOrder() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/acceptanceStandard")
    public ModelAndView acceptanceStandard() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/edit_acceptanceStandard")
    public ModelAndView edit_acceptanceStandard() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/edit_productionTaskOrder")
    public ModelAndView edit_productionTaskOrder() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/edit_standardPattern")
    public ModelAndView edit_standardPattern() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/edit_technicalGuidance")
    public ModelAndView edit_technicalGuidance() {
        return new ModelAndView();
    }


    @RequestMapping(value = "/addSteelplantStandardparts")
    @ResponseBody
    public ApiResult addSteelplantStandardparts(SteelplantStandardparts steelplantStandardparts, HttpServletRequest request) {

        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantStandardparts.setCreatorid(userId);
        steelplantStandardparts.setBeam_id(beamId);
        steelplantStandardparts.setCreatetime(new Date());
        int result = 0;
        try {
            result = steelplantProductionService.addSteelplantStandardparts(steelplantStandardparts);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }

    }

    @RequestMapping(value = "/delSteelplantStandardparts")
    @ResponseBody
    public ApiResult delSteelplantStandardparts(SteelplantStandardparts steelplantStandardparts, HttpServletRequest request) {

        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantStandardparts.setCreatorid(userId);
        steelplantStandardparts.setBeam_id(beamId);
        steelplantStandardparts.setCreatetime(new Date());
        int result = 0;
        try {
            result = steelplantProductionService.delSteelplantStandardparts(steelplantStandardparts);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }


    }

    @RequestMapping(value = "/updelSteelplantStandardparts")
    @ResponseBody
    public ApiResult updelSteelplantStandardparts(SteelplantStandardparts steelplantStandardparts, HttpServletRequest request) {
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantStandardparts.setCreatorid(userId);
        steelplantStandardparts.setBeam_id(beamId);
        steelplantStandardparts.setCreatetime(new Date());
        int result = 0;
        try {
            result = steelplantProductionService.updelSteelplantStandardparts(steelplantStandardparts);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }


    }

    @RequestMapping(value = "/querySteelplantStandardpartsAll")
    @ResponseBody
    public PageResult querySteelplantStandardpartsAll(SteelplantStandardpartsDTO steelplantStandardpartsDTO, HttpServletRequest request) {

        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantStandardpartsDTO.setCreatorid(userId);
        steelplantStandardpartsDTO.setBeam_id(beamId);
        steelplantStandardpartsDTO.setCreatetime(new Date());

        int count = steelplantProductionService.countSteelplantStandardpartsAll(steelplantStandardpartsDTO);
        List<Map> steelplantStandardpartss = steelplantProductionService.querySteelplantStandardpartsAll(steelplantStandardpartsDTO);
        return new PageResult(count, steelplantStandardpartsDTO.getOffset(), steelplantStandardpartss);

    }

    @RequestMapping(value = "/querySteelplantStandardpartsOne")
    @ResponseBody
    public SteelplantStandardparts querySteelplantStandardpartsOne(SteelplantStandardparts steelplantStandardparts, HttpServletRequest request) {
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        steelplantStandardparts.setBeam_id(beamId);
        return steelplantProductionService.querySteelplantStandardpartsOne(steelplantStandardparts);
    }


    @RequestMapping(value = "/addSteelplantProducetask")
    @ResponseBody
    public ApiResult addSteelplantProducetask(SteelplantProducetask steelplantProducetask, HttpServletRequest request) {

        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantProducetask.setCreatorid(userId);
        steelplantProducetask.setBeam_id(beamId);
        steelplantProducetask.setCreatetime(new Date());
        int result = 0;
        try {
            result = steelplantProductionService.addSteelplantProducetask(steelplantProducetask);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }

    }

    @RequestMapping(value = "/delSteelplantProducetask")
    @ResponseBody
    public ApiResult delSteelplantProducetask(SteelplantProducetask steelplantProducetask, HttpServletRequest request) {

        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantProducetask.setCreatorid(userId);
        steelplantProducetask.setBeam_id(beamId);
        steelplantProducetask.setCreatetime(new Date());
        int result = 0;
        try {
            result = steelplantProductionService.delSteelplantProducetask(steelplantProducetask);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }


    }

    @RequestMapping(value = "/updelSteelplantProducetask")
    @ResponseBody
    public ApiResult updelSteelplantProducetask(SteelplantProducetask steelplantProducetask, HttpServletRequest request) {
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantProducetask.setCreatorid(userId);
        steelplantProducetask.setBeam_id(beamId);
        steelplantProducetask.setCreatetime(new Date());
        int result = 0;
        try {
            result = steelplantProductionService.updelSteelplantProducetask(steelplantProducetask);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }


    }

    @RequestMapping(value = "/querySteelplantProducetaskAll")
    @ResponseBody
    public PageResult querySteelplantProducetaskAll(SteelplantProducetaskDTO steelplantProducetaskDTO, HttpServletRequest request) {

        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);


        steelplantProducetaskDTO.setCreatorid(userId);
        steelplantProducetaskDTO.setBeam_id(beamId);
        steelplantProducetaskDTO.setCreatetime(new Date());

        int count = steelplantProductionService.countSteelplantProducetaskAll(steelplantProducetaskDTO);
        List<Map> steelplantProducetasks = steelplantProductionService.querySteelplantProducetaskAll(steelplantProducetaskDTO);
        return new PageResult(count, steelplantProducetaskDTO.getOffset(), steelplantProducetasks);

    }

    @RequestMapping(value = "/querySteelplantProducetaskOne")
    @ResponseBody
    public SteelplantProducetask querySteelplantProducetaskOne(SteelplantProducetask steelplantProducetask, HttpServletRequest request) {
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        steelplantProducetask.setBeam_id(beamId);
        return steelplantProductionService.querySteelplantProducetaskOne(steelplantProducetask);
    }


    /*
   加工验收标准  文件夹遍历
    */
    @RequestMapping(value = "/queryAcceptanceStandardfilePath")
    @ResponseBody
    public List<AcceptanceStandardFilePathVo> queryAcceptanceStandardfilePath() {

        File file = new File(acceptanceStandardfilePath);
        List fileInfoList = new ArrayList();
        AcceptanceStandardFilePathVo acceptanceStandardFilePathVo = new AcceptanceStandardFilePathVo();
        return getFileList(file, acceptanceStandardFilePathVo).getChildren();
    }

    private AcceptanceStandardFilePathVo getFileList(File file, AcceptanceStandardFilePathVo acceptanceStandardFilePathVo_p) {

        File[] fs = file.listFiles();
        List<AcceptanceStandardFilePathVo> fileInfoList = new ArrayList<>();
        for (File f : fs) {

            if (f.isDirectory()) {
                AcceptanceStandardFilePathVo acceptanceStandardFilePathVo = new AcceptanceStandardFilePathVo();
                acceptanceStandardFilePathVo.setFileName(f.getName());
                acceptanceStandardFilePathVo.setFilePath(getUrlPath(f.getAbsolutePath()));
                acceptanceStandardFilePathVo.setFiletype("DIR");
                fileInfoList.add(getFileList(f, acceptanceStandardFilePathVo));
            }
            if (f.isFile()) {
                AcceptanceStandardFilePathVo acceptanceStandardFilePathVo_c = new AcceptanceStandardFilePathVo();
                acceptanceStandardFilePathVo_c.setFileName(f.getName());
                acceptanceStandardFilePathVo_c.setFilePath(getUrlPath(f.getAbsolutePath()));
                acceptanceStandardFilePathVo_c.setFiletype("FILE");
                fileInfoList.add(acceptanceStandardFilePathVo_c);
            }

        }
        acceptanceStandardFilePathVo_p.setChildren(fileInfoList);
        return acceptanceStandardFilePathVo_p;
    }

    private String getUrlPath(String filePath){
       return acceptanceStandardUrl+ filePath.replace(acceptanceStandardfilePath,"").replace("\\","/");
    }
   /* private  void getFileList(File file,String id, List fileInfoList){

        File[] fs = file.listFiles();
        for(File f:fs){
            AcceptanceStandardFilePathVo acceptanceStandardFilePathVo = new AcceptanceStandardFilePathVo();
            if(f.isDirectory())	{
                acceptanceStandardFilePathVo.setFileName(f.getName());
                acceptanceStandardFilePathVo.setFilePath(f.getAbsolutePath());
                acceptanceStandardFilePathVo.setFiletype("DIR");
                acceptanceStandardFilePathVo.setPid(id);
                String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                acceptanceStandardFilePathVo.setId(uuid);
                fileInfoList.add(acceptanceStandardFilePathVo);
                getFileList(f,uuid,fileInfoList);
            }
            if(f.isFile())	{
                acceptanceStandardFilePathVo.setFileName(f.getName());
                acceptanceStandardFilePathVo.setFilePath(f.getAbsolutePath());
                acceptanceStandardFilePathVo.setFiletype("FILE");
                acceptanceStandardFilePathVo.setPid(id);
                String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                acceptanceStandardFilePathVo.setId(uuid);
                fileInfoList.add(acceptanceStandardFilePathVo);
            }

        }
    }*/
}
